package com.xxl.job.admin.core.route;

import com.xxl.job.admin.core.route.strategy.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.biz.model.TriggerParam;

import java.util.Arrays;
import java.util.List;

public enum ExecutorRouteWithoutI18nStrategyEnum {

    FIRST("FIRST", new ExecutorRouteFirst()),
    LAST("LAST", new ExecutorRouteLast()),
    //轮询
    ROUND("ROUND", new ExecutorRouteRound()),
    RANDOM("RANDOM", new ExecutorRouteRandom()),
    CONSISTENT_HASH("CONSISTENT_HASH", new ExecutorRouteConsistentHash()),
    //最不经常使用
    LEAST_FREQUENTLY_USED("LEAST_FREQUENTLY_USED", new ExecutorRouteLFU()),
    //最近最久未使用
    LEAST_RECENTLY_USED("LEAST_RECENTLY_USED", new ExecutorRouteLRU()),
    //故障转移
    FAILOVER("FAILOVER", new ExecutorRouteFailover()),
    //忙碌转移
    BUSYOVER("BUSYOVER", new ExecutorRouteBusyover()),
    //分片广播
    SHARDING_BROADCAST("SHARDING_BROADCAST", null);

    ExecutorRouteWithoutI18nStrategyEnum(String title, ExecutorRouter router) {
        this.title = title;
        this.router = router;
    }

    private String title;
    private ExecutorRouter router;

    public String getTitle() {
        return title;
    }
    public ExecutorRouter getRouter() {
        return router;
    }

    public static ExecutorRouteWithoutI18nStrategyEnum match(String name, ExecutorRouteWithoutI18nStrategyEnum defaultItem){
        if (name != null) {
            for (ExecutorRouteWithoutI18nStrategyEnum item: ExecutorRouteWithoutI18nStrategyEnum.values()) {
                if (item.name().equals(name)) {
                    return item;
                }
            }
        }
        return defaultItem;
    }

    public static void main(String[] args) {

        String name = "CONSISTENT_HASH";
        TriggerParam triggerParam = new TriggerParam();
        triggerParam.setJobId(3);

        List<String> address = Arrays.asList("http://172.18.0.15:9999/", "http://172.18.0.8:9999/", "http://172.22.0.10:9999/","http://172.22.0.11:9999/");

        ExecutorRouteWithoutI18nStrategyEnum round = match(name, null);

        ExecutorRouter router = round.getRouter();
        ReturnT<String> returnT = router.route(triggerParam, address);

        ReturnT<String> returnT1 = router.route(triggerParam, address);
        System.out.println(returnT.getContent());
        System.out.println(returnT1.getContent());

    }

}
